package com.money.activemq;

import com.money.demo.common.Constants;
import com.money.demo.object.MoneyBag;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class MQConsumer {
    private static Connection connection;
    private static Session session;

    static {
        try {
            //1.创建ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Constants.MQ_URL);
            //2.创建Connection
            connection = connectionFactory.createConnection();
            //3.启动连接
            connection.start();
            //4.创建会话
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            e.printStackTrace();
        }


    }


    public static MessageConsumer consume(String name) throws JMSException {
        //5. 创建一个目标
        Destination destination = session.createQueue(name);
        //6. 创建一个消费者
        MessageConsumer consumer = session.createConsumer(destination);
        return consumer;

    }

}
